﻿using System;
using System.ComponentModel;
using Loki;
using Loki.Utils;
using Luna.Data.Security;

namespace Luna.Model.Security
{
    public class User : LunaEntity, IUser
    {
        public Guid PK
        {
            get { return ID; }
            set { ID = value; }
        }

        public string FullName
        {
            get { return string.Format(Resources.Formats.USER_FULLNAME, _Surname, _Name); }
        }

        #region  Name
        private static PropertyChangedEventArgs _NameChangeArgs = ObservableHelper.CreateArgs<User>(x => x.Name);

        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }

            set
            {
                if (value != _Name)
                {
                    _Name = value;
                    NotifyChangedAndDirty(_NameChangeArgs);
                }
            }
        }
        #endregion

        #region  Surname
        private static PropertyChangedEventArgs _SurnameChangeArgs = ObservableHelper.CreateArgs<User>(x => x.Surname);

        private string _Surname;

        public string Surname
        {
            get
            {
                return _Surname;
            }

            set
            {
                if (value != _Surname)
                {
                    _Surname = value;
                    NotifyChangedAndDirty(_SurnameChangeArgs);
                }
            }
        }
        #endregion

        #region  Login
        private static PropertyChangedEventArgs _LoginChangeArgs = ObservableHelper.CreateArgs<User>(x => x.Login);

        private string _Login;

        public string Login
        {
            get
            {
                return _Login;
            }

            set
            {
                if (value != _Login)
                {
                    _Login = value;
                    NotifyChangedAndDirty(_LoginChangeArgs);
                }
            }
        }
        #endregion

        #region  Hash
        private static PropertyChangedEventArgs _HashChangeArgs = ObservableHelper.CreateArgs<User>(x => x.Hash);

        private string _Hash;

        public string Hash
        {
            get
            {
                return _Hash;
            }

            set
            {
                if (value != _Hash)
                {
                    _Hash = value;
                    NotifyChangedAndDirty(_HashChangeArgs);
                }
            }
        }
        #endregion

        public void SetPassword(string P_Password)
        {
            Hash = HashHelper.ComputeHash(P_Password, HashAlgorithmType.SHA512);
        }

        public bool CheckPassword(string P_Password)
        {
            return HashHelper.VerifyHash(P_Password, HashAlgorithmType.SHA512, Hash);
        }

        public User()
            : base()
        {
        }

        internal User(User P_User)
            : base((LunaEntity)P_User)
        {
            LoadData(P_User);
        }

        public User(IUserData P_UserData)
            : base(P_UserData)
        {
            LoadData(P_UserData);
        }

        public void CopyValues(IUser P_User)
        {
            Name = P_User.Name;
            Surname = P_User.Surname;
            Login = P_User.Login;
            Hash = P_User.Hash;
        }

        private void LoadData(IUserData P_UserData)
        {
            _Name = P_UserData.Name;
            _Surname = P_UserData.Surname;
            _Login = P_UserData.Login;
            _Hash = P_UserData.Hash;
        }
    }
}
